package org.jeecg.modules.basic.job;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.jeecg.common.util.DateUtils;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.basic.attendance.entity.LwgyInoutAttendance;
import org.jeecg.modules.basic.attendance.service.ILwgyInoutAttendanceService;
import org.jeecg.modules.basic.demo.bed.entity.LwgyBaseBed;
import org.jeecg.modules.basic.demo.bed.service.ILwgyBaseBedService;
import org.jeecg.modules.basic.demo.student.entity.LwgyBaseStudent;
import org.jeecg.modules.basic.demo.student.service.ILwgyBaseStudentService;
import org.jeecg.modules.basic.entry.entity.LwgyAbnormalAction;
import org.jeecg.modules.basic.entry.service.ILwgyAbnormalActionService;
import org.jeecg.modules.basic.entry.service.ILwgyInoutRecordService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * TODO
 *
 * @author sn
 * @date 2022-09-23 15:20
 *  //考勤异常界定。一小时一次，或者半小时一次，查询当天，按照时间判断是正常还是晚归
 *
 */
@Slf4j
public class UnusualKqInfoJob implements Job {


    @Autowired
    private ILwgyBaseStudentService lwgyBaseStudentService;
    @Autowired
    private ILwgyInoutAttendanceService lwgyInoutAttendanceService;


    @SneakyThrows
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        log.info(String.format(" 时间:" + DateUtils.getTimestamp()));
        // 获取昨天的日期
        LocalDate today = LocalDate.now();
        // 构造昨天10点的时间
        LocalDateTime yesterdayTenAM = LocalDateTime.of(today, LocalTime.of(22, 0));
        // 格式化日期时间
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        // 构建 QueryWrapper
        QueryWrapper<LwgyInoutAttendance> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id"); // 指定要查询的字段
        queryWrapper.select("student_name"); // 指定要查询的字段
        queryWrapper.select("student_number"); // 指定要查询的字段
        queryWrapper.select("student_campus"); // 指定要查询的字段
        queryWrapper.select("student_college"); // 指定要查询的字段
        queryWrapper.select("student_major"); // 指定要查询的字段
        queryWrapper.select("student_class"); // 指定要查询的字段
        queryWrapper.select("student_year"); // 指定要查询的字段
        queryWrapper.select("position"); // 指定要查询的字段
        queryWrapper.select("sign_time"); // 指定要查询的字段
        queryWrapper.select("sign_result"); // 指定要查询的字段
        queryWrapper.isNull("sign_result");
        queryWrapper.likeRight("sign_time", today);

        //今天凌晨1点执行（比如）查询昨天所有的打卡记录
        List<LwgyInoutAttendance> kqlist = lwgyInoutAttendanceService.list(queryWrapper);
        List<LwgyBaseStudent> userList = new ArrayList<>();
        System.out.println("======:" + kqlist.size());
        if (kqlist.size() > 0) {
            // 判断是否晚归
            for (LwgyInoutAttendance record : kqlist) {

                LocalDateTime currentTime = LocalDateTime.parse(record.getSignTime().toString(), formatter);
                if (currentTime.isAfter(yesterdayTenAM)) {
                    System.out.println("晚归打卡： " + record.getSignTime());
                    record.setSignResult("1");
                } else {
                    record.setSignResult("0");
                }

            }
            lwgyInoutAttendanceService.updateBatchById(kqlist);

        }
    }
}
